//-----------------------------------------------------------------------------
//----- WindoTypeDlg.cpp : implementation file
#include "StdAfx.h"
#include "StdArx.h"
#include "resource.h"
#include "WindoTypeDlg.h"

//-----------------------------------------------------------------------------
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//-----------------------------------------------------------------------------
IMPLEMENT_DYNAMIC (CWindoTypeDlg, CAcUiDialog)

BEGIN_MESSAGE_MAP(CWindoTypeDlg, CAcUiDialog)
	//{{AFX_MSG_MAP(CWindoTypeDlg)
	ON_MESSAGE(WM_ACAD_KEEPFOCUS, OnAcadKeepFocus)    // Needed for modeless dialog.
	ON_EN_KILLFOCUS(IDC_EDIT_HEIGHT, OnKillfocusEditHeight)
	ON_EN_KILLFOCUS(IDC_EDIT_WIDTH, OnKillfocusEditWidth)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

//-----------------------------------------------------------------------------
CWindoTypeDlg::CWindoTypeDlg (CWnd* pParent /*=NULL*/, HINSTANCE hInstance /*=NULL*/) : CAcUiDialog (CWindoTypeDlg::IDD, pParent, hInstance) {
	//{{AFX_DATA_INIT(CWindoTypeDlg)
	m_nWindType = -1;
	m_nCols = windInfo.m_nCols;
	m_nRows = windInfo.m_nRows;
	//}}AFX_DATA_INIT
}

void CWindoTypeDlg::DoDataExchange (CDataExchange *pDX) {
	CAcUiDialog::DoDataExchange (pDX) ;
	//{{AFX_DATA_MAP(CWindoTypeDlg)
	DDX_Control(pDX, IDC_EDIT_HEIGHT, m_ctrlEdHeight);
	DDX_Control(pDX, IDC_EDIT_WIDTH, m_ctrlEdWidth);
	DDX_Radio(pDX, IDC_RB_TYPERECT, m_nWindType);
	DDX_Text(pDX, IDC_EDIT_COLS, m_nCols);
	DDV_MinMaxInt(pDX, m_nCols, 1, 10);
	DDX_Text(pDX, IDC_EDIT_ROWS, m_nRows);
	DDV_MinMaxInt(pDX, m_nRows, 1, 10);
	//}}AFX_DATA_MAP

}

// Needed for modeless dialogs to keep focus.
// Return FALSE to not keep the focus, return TRUE to keep the focus
LONG CWindoTypeDlg::OnAcadKeepFocus(UINT, UINT)
{
  return TRUE;
}

//-----------------------------------------------------------------------------

BOOL CWindoTypeDlg::OnInitDialog() 
{
	CAcUiDialog::OnInitDialog();
	
	// TODO: Add extra initialization here

	CButton *pWindoType;
	CString strWindType(windInfo.windType);
	if(strWindType == "Rect")
	{
		pWindoType = (CButton *) GetDlgItem(IDC_RB_TYPERECT);
		pWindoType->SetCheck(1);
	}
	else if(strWindType == "Arch")
	{
		pWindoType = (CButton *) GetDlgItem(IDC_RB_TYPEARCH);
		pWindoType->SetCheck(1);
	}
	else	// It must be and "Apex" type of window
	{
		pWindoType = (CButton *) GetDlgItem(IDC_RB_TYPEAPEX);
		pWindoType->SetCheck(1);
	}

	m_ctrlEdHeight.SetRange(10.00, 120.00);
	m_ctrlEdWidth.SetRange(10.00, 120.00);

	acdbRToS(windInfo.m_dWindHt, 2, 3, m_strHeight.GetBuffer(80));
	m_ctrlEdHeight.SetWindowText(m_strHeight);

	acdbRToS(windInfo.m_dWindWt, 2, 3, m_strWidth.GetBuffer(80));
	m_ctrlEdWidth.SetWindowText(m_strWidth);

	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CWindoTypeDlg::OnKillfocusEditHeight() 
{
	// TODO: Add your control notification handler code here
	m_ctrlEdHeight.Convert();
		if(!m_ctrlEdHeight.Validate())
	{
		::AfxMessageBox("Sorry, Range = 10.00 to 120.00");
		m_ctrlEdHeight.SetFocus();
		m_ctrlEdHeight.SetSel(0, -1);
	}
}

void CWindoTypeDlg::OnKillfocusEditWidth() 
{
	// TODO: Add your control notification handler code here
	m_ctrlEdWidth.Convert();
	if(!m_ctrlEdWidth.Validate())
	{
		::AfxMessageBox("Sorry, Range = 10.00 to 120.00");
		m_ctrlEdWidth.SetFocus();
		m_ctrlEdWidth.SetSel(0, -1);
	}
}

void CWindoTypeDlg::OnOK() 
{
	// TODO: Add extra validation here
	if (!UpdateData(TRUE))
	{
		return;
	}

	m_ctrlEdHeight.GetWindowText(m_strHeight);
	m_ctrlEdWidth.GetWindowText(m_strWidth);

	windInfo.m_dWindWt  = atof(m_strWidth);
	windInfo.m_dWindHt	= atof(m_strHeight);
	windInfo.m_nCols	= m_nCols;
	windInfo.m_nRows	= m_nRows;

	switch (m_nWindType)
	{
		case 0 :
			strcpy(windInfo.windType, "Rect");
		break;

		case 1 :
			strcpy(windInfo.windType, "Arch");
		break;

		case 2 :
			strcpy(windInfo.windType, "Apex");
		break;
	}
	
	CAcUiDialog::OnOK();
}
